package com.example.idlefish.Message

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.idlefish.PublicModule.ApplicationConst
import com.example.idlefish.PublicModule.UserConst
import com.example.idlefish.R

class MessageChatAdapter(private val messageList: MutableList<MessageChatData>, private val messagePreviewData: MessagePreviewData) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    class SenderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val messageContent: TextView = itemView.findViewById(R.id.message_main_chatItem_content_sender)
        val avatar: ImageView = itemView.findViewById(R.id.message_main_chatItem_avatar_sender)
    }

    class ReceiverViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val messageContent: TextView = itemView.findViewById(R.id.message_main_chatItem_content_receiver)
        val avatar: ImageView = itemView.findViewById(R.id.message_main_chatItem_avatar_receiver)
    }

    companion object {
        private const val VIEW_TYPE_SENDER = 1
        private const val VIEW_TYPE_RECEIVER = 2
    }

    override fun getItemViewType(position: Int): Int {
        // 假设 senderID 为  表示发送方，其他为接收方
        return if (messageList[position].senderID == UserConst.id.toInt()) VIEW_TYPE_SENDER else VIEW_TYPE_RECEIVER
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == VIEW_TYPE_SENDER) {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.message_chat_sender_item, parent, false)
            SenderViewHolder(view)
        } else {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.message_chat_receiver_item, parent, false)
            ReceiverViewHolder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val messageItem = messageList[position]
        if (holder is SenderViewHolder) {
            holder.messageContent.text = messageItem.messageContent
            // 设置头像等其他数据
            if (UserConst.avatar != null) {

                holder.avatar.setImageBitmap(UserConst.avatarImage)
            }
        } else if (holder is ReceiverViewHolder) {
            holder.messageContent.text = messageItem.messageContent
            // 设置头像等其他数据

            if (messagePreviewData.receiverAvatar != null) {
                val bitmap = ApplicationConst.decodeBase64ToImage(holder.itemView.context, messagePreviewData.senderAvatar)
                holder.avatar.setImageBitmap(bitmap)
            }
        }


    }

    override fun getItemCount(): Int {
        return messageList.size
    }

    fun updateMessages(newMessages: List<MessageChatData>) {
        val startPosition = messageList.size
        messageList.addAll(newMessages)
        notifyItemRangeInserted(startPosition, newMessages.size)
    }
}